﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.AnalysisInstance.Dtos;
using NP.BPMReportPlatform.AuthorizationUserManager;
using NP.BPMReportPlatform.AuthorizationUserManager.Dtos;
using NP.BPMReportPlatform.Reports.CommonApprovalProcessReport.Dtos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.AnalysisInstance
{
    /// <summary>
    /// 通用审批报表
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/AnalysisInstance")]
    public class AnalysisInstanceController : BPMReportPlatformController
    {
        private readonly IAnalysisInstanceService _service;
        private readonly IRoleAppService _roleAppService;
        public AnalysisInstanceController(IAnalysisInstanceService service, IRoleAppService roleAppService)
        {
            _service = service;
            _roleAppService = roleAppService;
        }

        [HttpPost("GetAnalysisInstances")]
        public async Task<PagedResultDto<Dictionary<string, object>>> GetAnalysisInstances(AnalysisInstanceQueryDto input)
        {
            return await _service.GetAnalysisInstances(input);
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ExportAnalysisInstances")]
        public async Task<FileContentResult> ExportAnalysisInstances([FromBody] AnalysisInstanceQueryDto input)
        {
            var data = await _service.ExportAnalysisInstances(input);
            var ms = new MemoryStream();
            data.Write(ms);
            data.Close();
            return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }

        /// <summary>
        /// 用户授权角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAuthAnalysisProcessTree")]
        public async Task<ProcessTreeData> GetAuthAnalysisProcessTree()
        {
            return await _roleAppService.GetAuthAnalysisProcessTree();
        }

        /// <summary>
        /// 用户授权角色(三方流程页面)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAuthAnalysisProcessTreeForThirdFlow")]
        public async Task<ProcessTreeData> GetAuthAnalysisProcessTreeForThirdFlow()
        {
            return await _roleAppService.GetAuthAnalysisProcessTreeForThirdFlow();
        }
    }
}
